package com.home.auto_navigation.ui.pages;

import java.util.HashMap;
import java.util.Map;

import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import com.home.auto_navigation.common.AutoNavigationFileDecorator;
import com.home.auto_navigation.common.api.NavigationLink;
import com.home.auto_navigation.ui.AbstractPageObject;
import com.home.auto_navigation.ui.controllers.Button;

public class LoginPage extends AbstractPageObject {
	@FindBy(id = "sso_username")
	private WebElement username;
	@FindBy(id = "ssopassword")
	private WebElement password;
	@NavigationLink(successor = HomeAfterLoginPage.class)
	@FindBy(className = "buttonLinkText")
	private Button singIn;

	public LoginPage() {
		PageFactory.initElements(new AutoNavigationFileDecorator(getDriver()), this);
		this.successors = new HashMap<Class<? extends AbstractPageObject>, Button>();
		this.successors.put(HomeAfterLoginPage.class, singIn);
	}

	@Override
	public boolean isExist() {
		try {
			return username.isDisplayed();
		} catch (NoSuchElementException e) {
			return false;
		}
	}

	@Override
	public void invokeSuccessor(Map<String, String> businessData,
			Class<? extends AbstractPageObject> page) {
		if (businessData != null) {
			if (page.equals(HomeAfterLoginPage.class)) {
				invokeHomeAfterLoginPage(businessData);
			}
		}
	}

	private void invokeHomeAfterLoginPage(Map<String, String> businessData) {
		username.sendKeys(businessData.get("username"));
		password.sendKeys(businessData.get("password"));
	}
}
